package com.hsylient.javase.chapter09;

import com.hsylient.javase.common.enmu.Status;
import com.hsylient.javase.common.entity.Employee;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Collections;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class TestStreamGroupBy {

    @Test
    public void testStreamGroupBy() {
        // 准备原始的数据集合
        List<Employee> employees = List.of(new Employee(1L, "A", 12, 123.45, Status.FREE), new Employee(1L, "A", 13, 666.45, Status.FREE), new Employee(1L, "B", 14, 123.45, Status.FREE), new Employee(1L, "B", 15, 123.45, Status.FREE), new Employee(1L, "E", 16, 123.45, Status.FREE), new Employee(1L, "F", 17, 123.45, Status.FREE), new Employee(1L, "C", 18, 123.45, Status.FREE));

        // 将employ按照name分组
        Map<String, List<Employee>> collect = employees.stream().collect(Collectors.groupingBy(Employee::getName));
        log.info("[Stream分组] 按照name分组结果 {}", collect);

        // 先按照name分组，再按照id分组。
        Map<String, Map<Long, List<Employee>>> collect2 = employees.stream()
                .collect(Collectors.groupingBy(Employee::getName, Collectors.groupingBy(Employee::getId)));


        // 将employ按照name分组 然后对age这个字段进行求和、秋平均数、最大值、最小值的统计
        Map<String, IntSummaryStatistics> collect1 = employees.stream()
                .collect(Collectors.groupingBy(Employee::getName, Collectors.summarizingInt(Employee::getAge)));
        System.out.println(collect1);
    }
}
